<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose"
    :append-to-body="true"
  >
    <el-row>
      <el-form
        label-width="100px"
        ref="createForm"
        :rules="rules"
        :model="formData"
      >
        <el-form-item label="登录者密码" prop="adminPassword">
          <el-input v-model="formData.adminPassword" type="password" />
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="formData.newPassword" type="password" />
        </el-form-item>
        <el-form-item label="确认密码" prop="checkNewPassword">
          <el-input v-model="formData.checkNewPassword" type="password" />
        </el-form-item>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose()">取 消</el-button>
      <el-button type="primary" @click="comit()" :loading="loading"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import { ResetPassword } from '@api/systemManage/userManage'
export default {
  data () {
    return {
      title: '',
      dialogVisible: false,
      loading: false,
      formData: {
        userId: 0,
        adminPassword: '',
        newPassword: '',
        checkNewPassword: ''
      },
      rules: {
        adminPassword: [
          { required: true, message: '请输入当前登录者账号的密码', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        checkNewPassword: [
          { required: true, message: '请输入确认密码', trigger: 'blur' }
        ]
      }

    }
  },
  methods: {
    comit () {
      this.$refs.createForm.validate((valid) => {
        if (valid) {
          if (this.formData.newPassword !== this.formData.checkNewPassword) {
            this.$message.error('新密码与确认密码不一致')
            return
          }
          this.loading = true
          ResetPassword(this.formData).then(res => {
            if (res.success) {
              this.handleClose()
              this.$notify({
                title: '提示',
                message: '修改成功',
                type: 'success'
              })
            }
          })
        }
      })
    },

    show (value, title) {
      this.formData.userId = value
      this.dialogVisible = true
      this.title = title
    },
    handleClose () {
      this.formData = {
        userId: 0,
        adminPassword: '',
        newPassword: ''
      }
      this.loading = false
      this.dialogVisible = false
      this.$emit('fromvisible', false)
    }
  }
}
</script>
